//
//  CCVSCell.m
//  ClinicalCollections
//
//  Created by Pasirstein, David B on 4/5/13.
//  Copyright (c) 2013 GOPI. All rights reserved.
//

#import "CCVSCell.h"
#import "CCVSCustomView.h"
#import "CCVSCommon.h"

@implementation CCVSCell

@synthesize vc = _vc;

@synthesize topics = _topics;
@synthesize details = _details;

-(void)setTopics:(NSArray *)topics
{
    _topics = topics;
    //if we already know the details then
    //we have enough to generate the cell
    if (_details) [self buildView];
}

-(void)setDetails:(NSArray *)details
{
    _details = details;
    //if we already know the topics
    //then we have enough to generate the cell
    if (_topics) [self buildView];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


//Create the view layout for the cell and
//add the necessary subviews
-(void)buildView
{
    [self cleanUpCustomViews]; //make sure we have a clean slate
    CCVSCustomView * ccvs = [[CCVSCustomView alloc] init];
    ccvs.vc = self.vc;
    ccvs.superView = self;
    ccvs.topics = self.topics;
    ccvs.details = self.details;
    [ccvs buildView];
    //that should be it (it will auto build the subviews AND
    //add itself to this view
}


-(void)resetCell
{
    self.topics = nil;
    self.details = nil;
    self.vc = nil;
    [self cleanUpCustomViews];
}

//Re-use of this cell could happen do to a change
//or the cell could be recycled.  If this happens
//we need to clean up as we don't want the old subviews
//and related data sticking around
-(void)cleanUpCustomViews
{
    for (int i = 0; i<[self.subviews count]; i++) {
        if ([self.subviews[i] isKindOfClass:[CCVSCustomView class]]) {
            [self.subviews[i] removeFromSuperview];
        }
    }
}


//Dynamically determine how large in height this custom cell should be
//given a known width by adding up the contents of the topics and details
//arrays and taking the MAX() for each row + defined boarders
+(CGFloat)calcHeightOfCell:(NSArray *)topics withDetails:(NSArray*)details withCellWidth:(CGFloat)cellWidth
{
    CGFloat height = 0;
    if ((id)topics != [NSNull null]) {
        height = [CCVSCustomView calcSizeOfLabel:@[topics,details]
                                  withLabelWidth:cellWidth].height;
        height += CCVS_BOARDERSIZE * [topics count];
    }
    return height;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
